package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysMenuApp;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysMenuAppMapper;
import com.ruoyi.system.service.ISysMenuAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * APP权限Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-17
 */
@Service
public class SysMenuAppServiceImpl implements ISysMenuAppService 
{
    @Autowired
    private SysMenuAppMapper sysMenuAppMapper;

    /**
     * 查询APP权限
     * 
     * @param menuId APP权限主键
     * @return APP权限
     */
    @Override
    public SysMenuApp selectSysMenuAppByMenuId(Long menuId)
    {
        return sysMenuAppMapper.selectSysMenuAppByMenuId(menuId);
    }

    /**
     * 查询APP权限列表
     * 
     * @param sysMenuApp APP权限
     * @return APP权限
     */
    @Override
    public List<SysMenuApp> selectSysMenuAppList(SysMenuApp sysMenuApp)
    {
        return sysMenuAppMapper.selectSysMenuAppList(sysMenuApp);
    }

    /**
     * 新增APP权限
     * 
     * @param sysMenuApp APP权限
     * @return 结果
     */
    @Override
    public int insertSysMenuApp(SysMenuApp sysMenuApp)
    {
        sysMenuApp.setCreateTime(DateUtils.getNowDate());
        return sysMenuAppMapper.insertSysMenuApp(sysMenuApp);
    }

    /**
     * 修改APP权限
     * 
     * @param sysMenuApp APP权限
     * @return 结果
     */
    @Override
    public int updateSysMenuApp(SysMenuApp sysMenuApp)
    {
        sysMenuApp.setUpdateTime(DateUtils.getNowDate());
        return sysMenuAppMapper.updateSysMenuApp(sysMenuApp);
    }

    /**
     * 批量删除APP权限
     * 
     * @param menuIds 需要删除的APP权限主键
     * @return 结果
     */
    @Override
    public int deleteSysMenuAppByMenuIds(Long[] menuIds)
    {
        return sysMenuAppMapper.deleteSysMenuAppByMenuIds(menuIds);
    }

    /**
     * 删除APP权限信息
     * 
     * @param menuId APP权限主键
     * @return 结果
     */
    @Override
    public int deleteSysMenuAppByMenuId(Long menuId)
    {
        return sysMenuAppMapper.deleteSysMenuAppByMenuId(menuId);
    }

    public List<SysMenuApp> buildMenuTree(List<SysMenuApp> menus) {
        List<SysMenuApp> returnList = new ArrayList<SysMenuApp>();
        List<Long> tempList = menus.stream().map(SysMenuApp::getMenuId).collect(Collectors.toList());
        for (Iterator<SysMenuApp> iterator = menus.iterator(); iterator.hasNext(); ) {
            SysMenuApp menu = (SysMenuApp) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId())) {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param menus 菜单列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<SysMenuApp> menus) {
        List<SysMenuApp> menuTrees = buildMenuTree(menus);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     *
     * @param list 分类表
     * @param t    子节点
     */
    private void recursionFn(List<SysMenuApp> list, SysMenuApp t) {
        // 得到子节点列表
        List<SysMenuApp> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysMenuApp tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysMenuApp> getChildList(List<SysMenuApp> list, SysMenuApp t) {
        List<SysMenuApp> tlist = new ArrayList<>();
        Iterator<SysMenuApp> it = list.iterator();
        while (it.hasNext()) {
            SysMenuApp n = (SysMenuApp) it.next();
            if (n.getParentId().longValue() == t.getMenuId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysMenuApp> list, SysMenuApp t) {
        return getChildList(list, t).size() > 0;
    }
}
